#! /usr/bin/sdlbrt

finp$="polynesia/_scr/04.scr"

sub byteplot(x,y,v,i,p)
  for z=0 to 7:ik=p:z2=7-z   
    if (((int(v/(2^z2)))mod 2) and (2^z2)) then:ik=i:end if
    ink(ik):dot(x+z,y): next
  end sub

function p8bit2rgb(c)
  return ( ( (int(bitwiseand(c,7)*255/7))*65536 ) + ( (int((bitwiseand(c,54)/8)*255/7))*256 ) + ( (int((bitwiseand(c,192)/64)*255/3)))  )
  end function

sub filpic(qxx1,qyy1,qstq$)
  yyt=qyy1:xxt=qxx1:tstt$=qstq$
  for qq=0 to len(tstt$)-1
    qu=(((val(mid$(tstt$,qq+1,1)))*255)/4):ink(rgb(qu,qu,qu)):dot(xxt+qq,yyt):next
  end sub
  
function chridtoarrid(a)
  a1=bitwiseand(a,128)/2
  a2=bitwiseand(a,63)
  return bitwiseor(a1,a2)
  end function

function bthx$(a)
  b$="00"+hex$(a):bthx$=right$(b$,2)
  end function


'finp$="utomhusb_2.scr.png"
onlgr=1

dim mq$[128],vmem[800],txou$[4],vram[6144],atributos[768],paleta[64]

finp2$="_tmpr_m.png"
shell("convert -modulate 100,0 "+finp$+" "+finp2$)

xed=128:yed=96

yed2=int(yed*3):xed2=int(xed*.5):if yed/2<128 then:yed2=yed+128:end if
xedq=xed*2:if xedq/2<128 then:xedq=256:end if

setdisplay(512,192+64,32,1):paper(0xCC8844):ink(0):pen(0):cls

open finp$ for input as #1
for iq=0 to 6143:vram[iq]=readbyte(1):next
for iq=0 to 767:atributos[iq]=readbyte(1):next
for iq=0 to 63:paleta[iq]=readbyte(1):next
close #1

'open finp$+"_vram.bin" for output as #1:for iq=0 to 6143:writebyte(1,vram[iq]):next:close #1
'open finp$+"_atributos.bin" for output as #1:for iq=0 to 767:writebyte(1,atributos[iq]):next:close #1
'open finp$+"_paleta.bin" for output as #1:for iq=0 to 63:writebyte(1,paleta[iq]):next:close #1

setdisplay(512,384,32,1):paper(8^8-1):ink(0):pen(0):cls

for y3=0 to 2
  for y2=0 to 7
    for x2=0 to 31
      atribv=atributos[(y3*256)+(y2*32)+x2]
      for y1=0 to 7
        byteplot(256+(x2*8),(y3*64)+(y1*8)+y2,vram[0+x2+(y1*32)+(y2*256)+(y3*2048)],0x000000,0xFFFFFF )
        'qv1=p8bit2rgb(paleta[bitwiseand(atribv,7)])
        'qv2=p8bit2rgb(paleta[bitwiseand(atribv,54)/8])
        'byteplot(256+(x2*8),(y3*64)+(y1*8)+y2,vram[0+x2+(y1*32)+(y2*256)+(y3*2048)],qv1,qv2 )
        next:next:next:next 

'loadimage("pic1.png",1):pasteicon(0,0,1)
'grab(1,0,0,256,192):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+".png && rm _.bmp")


'- antigamente carregava imagens...
'- loadimage(finp2$,1): pasteicon(256,0,1)

mq$[00]=" ":mq$[01]="\\\' ":mq$[02]="\\ \'":mq$[03]="\\\'\'":mq$[04]="\\. ":mq$[05]="\\: ":mq$[06]="\\.'":mq$[07]="\\:'"
mq$[08]="\\##":mq$[09]="\\,,":mq$[10]="\\~~"

mq$[11]="\\\""

'- mq$[11]="\\\"\\\""
'- mq$[11]="\\\"+chr$(11)+\\\""

mq$[12]="#":mq$[13]="$":mq$[14]=":":mq$[15]="?"
mq$[16]="(":mq$[17]=")":mq$[18]=">":mq$[19]="<":mq$[20]="=":mq$[21]="+":mq$[22]="-":mq$[23]="*"
mq$[24]="/":mq$[25]=";":mq$[26]=",":mq$[27]=".":mq$[28]="0":mq$[29]="1":mq$[30]="2":mq$[31]="3"
mq$[32]="4":mq$[33]="5":mq$[34]="6":mq$[35]="7":mq$[36]="8":mq$[37]="9":mq$[38]="A":mq$[39]="B"
mq$[40]="C":mq$[41]="D":mq$[42]="E":mq$[43]="F":mq$[44]="G":mq$[45]="H":mq$[46]="I":mq$[47]="J"
mq$[48]="K":mq$[49]="L":mq$[50]="M":mq$[51]="N":mq$[52]="O":mq$[53]="P":mq$[54]="Q":mq$[55]="R"
mq$[56]="S":mq$[57]="T":mq$[58]="U":mq$[59]="V":mq$[60]="W":mq$[61]="X":mq$[62]="Y":mq$[63]="Z"
mq$[64+00]="% ":mq$[64+01]="\\.:":mq$[64+02]="\\:.":mq$[64+03]="\\..":mq$[64+04]="\\\':":mq$[64+05]="\\ :":mq$[64+06]="\\\'.":mq$[64+07]="\\ ."
mq$[64+08]="\\@@":mq$[64+09]="\\;;":mq$[64+10]="\\!!":mq$[64+11]="%\"":mq$[64+12]="%#":mq$[64+13]="%$":mq$[64+14]="%:":mq$[64+15]="%?"
mq$[64+16]="%(":mq$[64+17]="%)":mq$[64+18]="%>":mq$[64+19]="%<":mq$[64+20]="%=":mq$[64+21]="%+":mq$[64+22]="%-":mq$[64+23]="%*"
mq$[64+24]="%/":mq$[64+25]="%;":mq$[64+26]="%,":mq$[64+27]="%.":mq$[64+28]="%0":mq$[64+29]="%1":mq$[64+30]="%2":mq$[64+31]="%3"
mq$[64+32]="%4":mq$[64+33]="%5":mq$[64+34]="%6":mq$[64+35]="%7":mq$[64+36]="%8":mq$[64+37]="%9":mq$[64+38]="%A":mq$[64+39]="%B"
mq$[64+40]="%C":mq$[64+41]="%D":mq$[64+42]="%E":mq$[64+43]="%F":mq$[64+44]="%G":mq$[64+45]="%H":mq$[64+46]="%I":mq$[64+47]="%J"
mq$[64+48]="%K":mq$[64+49]="%L":mq$[64+50]="%M":mq$[64+51]="%N":mq$[64+52]="%O":mq$[64+53]="%P":mq$[64+54]="%Q":mq$[64+55]="%R"
mq$[64+56]="%S":mq$[64+57]="%T":mq$[64+58]="%U":mq$[64+59]="%V":mq$[64+60]="%W":mq$[64+61]="%X":mq$[64+62]="%Y":mq$[64+63]="%Z"

txou$[0]="":txou$[1]="":txou$[2]="":txou$[3]=""

filpic(0,(yed*2)+ 0,"44444444000044444444000000000000444444440000444444440000000000000404040444444444040404044444444444444444444444444444444444444444")
filpic(0,(yed*2)+ 1,"44444444000044444444000000000000444444440000444444440000000000004040404044444444404040404404404444400044444404444444444444000044")
filpic(0,(yed*2)+ 2,"44444444000044444444000000000000444444440000444444440000000000000404040444444444040404044404404444044404440000044444444440444404")
filpic(0,(yed*2)+ 3,"44444444000044444444000000000000444444440000444444440000000000004040404044444444404040404444444440000444440404444440444444444044")
filpic(0,(yed*2)+ 4,"44444444444444444444444444444444000044440000444400004444000044440404040404040404444444444444444444044444440000044444444444440444")
filpic(0,(yed*2)+ 5,"44444444444444444444444444444444000044440000444400004444000044444040404040404040444444444444444444044444444404044444444444444444")
filpic(0,(yed*2)+ 6,"44444444444444444444444444444444000044440000444400004444000044440404040404040404444444444444444440000004440000044440444444440444")
filpic(0,(yed*2)+ 7,"44444444444444444444444444444444000044440000444400004444000044444040404040404040444444444444444444444444444404444444444444444444")
filpic(0,(yed*2)+ 8,"44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+ 9,"44444044440444444444444444444444444444444444444444444444444444444444444444444444444444444444444444000044444004444400004444000044")
filpic(0,(yed*2)+10,"44440444444044444440444444444044444444444444044444444444444040444444440444404444444444444444444440444004440404444044440440444404")
filpic(0,(yed*2)+11,"44440444444044444444044444440444440000044444044444444444444404444444404444444444444444444444444440440404444404444444440444440044")
filpic(0,(yed*2)+12,"44440444444044444444404444404444444444444400000444000004440000044444044444444444444444444444444440404404444404444400004444444404")
filpic(0,(yed*2)+13,"44440444444044444444044444440444440000044444044444444444444404444440444444404444444404444440044440044404444404444044444440444404")
filpic(0,(yed*2)+14,"44444044440444444440444444444044444444444444044444444444444040444404444444404444444404444440044444000044440000044000000444000044")
filpic(0,(yed*2)+15,"44444444444444444444444444444444444444444444444444444444444444444444444444044444444044444444444444444444444444444444444444444444")
filpic(0,(yed*2)+16,"44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+17,"44440444400000044400004440000004440000444400004444000044400000444400004440000444400000044000000444000044404444044400000444444404")
filpic(0,(yed*2)+18,"44400444404444444044444444444404404444044044440440444404404444044044440440444044404444444044444440444404404444044444044444444404")
filpic(0,(yed*2)+19,"44040444400000444000004444444044440000444044440440444404400000444044444440444404400000444000004440444444400000044444044444444404")
filpic(0,(yed*2)+20,"40440444444444044044440444440444404444044400000440000004404444044044444440444404404444444044444440440004404444044444044440444404")
filpic(0,(yed*2)+21,"40000004404444044044440444404444404444044444440440444404404444044044440440444044404444444044444440444404404444044444044440444404")
filpic(0,(yed*2)+22,"44440444440000444400004444404444440000444400004440444404400000444400004440000444400000044044444444000044404444044400000444000044")
filpic(0,(yed*2)+23,"44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+24,"44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+25,"40444044404444444044440440444404440000444000004444000044400000444400004400000004404444044044440440444404404444040444440440000004")
filpic(0,(yed*2)+26,"40440444404444444004400440044404404444044044440440444404404444044044444444404444404444044044440440444404440440444044404444444044")
filpic(0,(yed*2)+27,"40004444404444444040040440404404404444044044440440444404404444044400004444404444404444044044440440444404444004444404044444440444")
filpic(0,(yed*2)+28,"40440444404444444044440440440404404444044000004440404404400000444444440444404444404444044044440440444404444004444440444444404444")
filpic(0,(yed*2)+29,"40444044404444444044440440444004404444044044444440440404404440444044440444404444404444044404404440400404440440444440444444044444")
filpic(0,(yed*2)+30,"40444404400000044044440440444404440000444044444444000044404444044400004444404444440000444440044444044044404444044440444440000004")
filpic(0,(yed*2)+31,"44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")

for y1=0 to 31:for x1=0 to 127:u=point(x1,(yed*2)+y1):ink(0xFFFFFF-u):dot(x1,(yed*2)+y1+32):next:next
for y1=64 to 79:for x1=0 to 127:u=point(x1,(yed*2)+y1):ink(0xFFFFFF-u):dot(x1,(yed*2)+y1+16):next:next

'open finp$+".bin" for output as #1

vmec=0
for y1=0 to 191 step 8:for x1=0 to 255 step 8:dfi=65536:fgx=0:fgy=0
  for yq=0 to 7:for xq=0 to 15:cnt=0
    for y2=0 to 7:for x2=0 to 7
      q1=bitwiseand((point(256+x1+x2,0+y1+y2)),255) 
      q2=bitwiseand((point(0+(xq*8)+x2,192+(yq*8)+y2)),255)
      dq=abs(q2-q1):cnt=cnt+dq
      next:next
    if cnt<dfi then:dfi=cnt:fgx=xq:fgy=yq:end if
    next:next

chrv=(fgy*16)+fgx
if chrv>=64 then:chrv+=64:end if
'writebyte (1,chrv)
vmem[vmec]=chrv

  for y2=0 to 7:for x2=0 to 7
    c=point((fgx*8)+x2,(yed*2)+(fgy*8)+y2):ink(c):dot(x1+x2,y1+y2)
    next:next
  vmec=vmec+1
  next:next
fout$=finp$+".bmp":grab (1,0,0,xed*2,yed*2):saveimage (fout$,1)
'- unix only (?) - needs imagemagick installed
shell("convert "+fout$+" "+fout$+".png"):shell("rm "+fout$+" "+finp2$)

'close #1

setcaption ("fill print array 0")
for i=0 to 255
  txou$[0]=txou$[0]+mq$[ chridtoarrid(vmem[i]) ]
  next
setcaption ("fill print array 1")
for i=0 to 255
  txou$[1]=txou$[1]+mq$[ chridtoarrid(vmem[i+256]) ]
  next
setcaption ("fill print array 2")
for i=0 to 191
  txou$[2]=txou$[2]+mq$[ chridtoarrid(vmem[i+512]) ]
  next
setcaption ("fill print array 3")
for i=0 to 63
  txou$[3]=txou$[3]+mq$[ chridtoarrid(vmem[i+704]) ]
  next

'open finp$+".bas" for output as #1
'print #1,"  10 FAST"
'print #1,"  11 CLS"
'print #1,"  12 CLEAR"
'print #1,"  15 PRINT AT 0,0;\""+txou$[3]+"\""
'print #1,"  18 let d=1+peek 16396+256*peek 16397"
'print #1,"  20 for f=0 to 63"
'print #1,"  21 let g=f+704"
'print #1,"  22 poke d+g+int(g/32),peek(d+f+int(f/32))"
'print #1,"  23 next f"
'print #1,"  30 PRINT AT 0,0;\""+txou$[0]+"\""
'print #1,"  31 PRINT \""+txou$[1]+"\""
'print #1,"  32 PRINT \""+txou$[2]+"\""
'print #1,"  40 SLOW"
'print #1,"  41 PAUSE 4E4"
'close #1

setcaption ("tokenizing and opening emulator")

shell("zxtext2p -o "+finp$+".p "+finp$+".bas")
shell("sz81 "+finp$+".p")

'open finp$+"_vmem.bin" for output as #1:for iq=0 to 767:writebyte(1,vmem[iq]):next:close #1


open finp$+"_v.bin" for output as #1
for iq=0 to 767:writebyte(1,vmem[iq]):next
for iq=0 to 767:writebyte(1,atributos[iq]):next
for iq=0 to 63:writebyte(1,paleta[iq]):next
close #1

open finp$+"_v.hex" for output as #1
open finp$+"_v.bin" for input as #2
print #1,"hexdump01:"
print #1,"asm"
  a$="":ctz=0
  while not eof(2)
    aln=len(a$)
    if ctz>=16 then:print #1,"  defb "+a$:a$="":ctz=0:end if
    u=readbyte(2):ctz=ctz+1:a$=a$+"0"+bthx$(u)+"h"
    if ctz<16 then:a$=a$+",":end if
    end while
  aln=len(a$):if aln>0 then:print #1,"  defb "+a$:a$="":end if
print #1,"  end asm"
close #1:close #2

'- waitkey
